package com.aruba.girlanime.delegate;

import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.Random;

/**
 * 男孩女孩动画
 * Created by aruba on 2018/4/17.
 */

public class GirlAnimeDelegate implements IAnimeDelegate {
    private RelativeLayout rl_main;
    private LinearLayout iv_girl;
    private ImageView iv_dialog_right;
    private ImageView iv_dialog_left;
    private ImageView iv_man;

    private float width;
    private float height;
    /**
     * 女孩移动高度的系数
     */
    private float ratio = 0.5f;
    private float girl_x;
    //y轴移动的距离
    private float scroll_y;
    ValueAnimator valueAnimator;

    private int speed = 2;

    @Override
    public void startAnime() {
        if (girl_x == 0)
            girl_x = iv_girl.getX();
        if (scroll_y == 0) {
            scroll_y = height * ratio - iv_girl.getHeight() / 2;
        }

        reset();
        if (valueAnimator == null) {
            valueAnimator = ValueAnimator.ofFloat(0, 1);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setDuration(3000);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float progress = (float) animation.getAnimatedValue();
                    float catchProgress = (progress - 0.5f) * speed;
                    if (catchProgress > 0.5f) {
                        catchProgress = 0.5f;
                    }

                    if (progress < 0.5) {
                        //执行女孩左上移动
                        iv_girl.setTranslationX((width - girl_x) * progress);
                        iv_girl.setTranslationY(-scroll_y * progress * 2);
                    } else if (progress < 0.75 && catchProgress != 0.5f) {
                        //显示左侧对话框
                        if (iv_dialog_right.getVisibility() != View.VISIBLE) {
                            iv_dialog_right.setVisibility(View.VISIBLE);
//                            iv_dialog_right.setTranslationX((width - girl_x) * 0.5f);
//                            iv_dialog_right.setTranslationY(-scroll_y);
                        }

                        //男的右移
                        iv_man.setTranslationX((-iv_man.getWidth() * 2 / 3) * (1 - catchProgress));
                    } else if (iv_dialog_left.getVisibility() != View.VISIBLE) {
                        //男的右移
                        iv_man.setTranslationX((-iv_man.getWidth() * 2 / 3) * (1 - catchProgress));

                        //显示左边的dialog
                        iv_dialog_left.setVisibility(View.VISIBLE);
                        iv_dialog_left.setTranslationX((-iv_man.getWidth() * 2 / 3) * (1 - (progress - 0.5f)));
                    }
                }
            });
        }

        if (!valueAnimator.isRunning()) {
            speed = new Random().nextInt(2) * 4 + 2;
            Log.i("anime", speed + "");
            valueAnimator.start();
        }
    }

    @Override
    public void init(View... args) {
        rl_main = (RelativeLayout) args[0];
        iv_girl = (LinearLayout) args[1];
        iv_dialog_right = (ImageView) args[2];
        iv_dialog_left = (ImageView) args[3];
        iv_man = (ImageView) args[4];

        iv_man.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (width == 0) {
                    initDimens();

                    //左移隐藏
                    iv_man.setTranslationX(-iv_man.getWidth() * 2 / 3);
                    iv_man.setTranslationY(height * ratio - iv_man.getHeight() / 2);

                    iv_dialog_left.setTranslationX(-iv_man.getWidth() * 2 / 3);
                    iv_dialog_left.setTranslationY(height * ratio - iv_man.getHeight());
                    iv_dialog_left.setRotationY(180);
                }
            }
        });
    }

    private void initDimens() {
        width = rl_main.getWidth();
        height = rl_main.getHeight();
    }

    @Override
    public void stopAnime() {
        valueAnimator.cancel();
    }

    private void hide() {
        iv_dialog_right.setVisibility(View.INVISIBLE);
        iv_dialog_left.setVisibility(View.INVISIBLE);
    }

    @Override
    public void reset() {
        hide();

        iv_man.setTranslationX(-iv_man.getWidth() * 2 / 3);
        iv_man.setTranslationY(height * ratio - iv_man.getHeight() / 2);
        iv_girl.setTranslationX(0);
        iv_girl.setTranslationY(0);
        iv_dialog_left.setTranslationX(-iv_man.getWidth() * 2 / 3);
        iv_dialog_left.setTranslationY(height * ratio - iv_man.getHeight());
    }
}
